package com.eptok.weixin.action;


import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.eptok.weixin.util.WxCooikeManager;

import hikefa.core.remote.client.RemoteCall;
import hikefa.core.util.StringUtil;
import hikefa.core.web.page.Pager;
import hikefa.core.web.page.Paginater;
import yspay.express.bean.Result;
import yspos.cpfs.express.entity.ExpBizMerchant;
import yspos.cpfs.express.entity.ExpUserinfo;
import yspos.cpfs.express.entity.Query;
import yspos.cpfs.remoteserver.iface.IExpressService;

/**
 * 
*企业类
* <p>Title: CompanyController</p>
* <p>Description: </p>
* <p>Company: </p> 
* @author:邓磊
* @date:上午11:12:42
 */
@Controller
@RequestMapping("/weixin/user")
public class CompanyController  extends BaseDispatchAction{
	
	private IExpressService expressServer = (IExpressService) RemoteCall.getRemoteObject(IExpressService.class);
	
	
	/*
	 * 搜索企业
	 */
	@RequestMapping(value = { "/companySearch.do" })    
	public ModelAndView searchCompany(HttpServletRequest request, HttpServletResponse response,
			Query q,Pager page) 
	{
		//根据关键字查找我没有加入的企业
		page.setPageSize(5);
		q.setLat(WxCooikeManager.getCookieValue("point.lat", request));
		q.setLng(WxCooikeManager.getCookieValue("point.lng", request));
		Paginater pageer = null;
		try {
			ExpUserinfo user = WxCooikeManager.getUser(request);
			q.setUserId(user.getId());
			pageer = expressServer.getBizMerchantNotJoin(q, page);
			request.setAttribute("objs", pageer);
			request.setAttribute("q", q);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return new ModelAndView(PATH+"/company/companySearch");
	}
	
	
	/*
	 * 申请加入企业(通过商户号加入)
	 */
	@RequestMapping(value = { "/companyJoin.do" })
	@ResponseBody
	public Result companyJoinById(HttpServletRequest request, HttpServletResponse response,
			String mercNo,String remark,String invId) 
	{
		Result ret = null;
		ExpBizMerchant merc = expressServer.getBizMerchantByNo(mercNo);
		if(null == merc)
		{
			return Result.init("1", "商户号不存在");
		}
		ExpUserinfo user = WxCooikeManager.getUser(request);
		try {
			if(StringUtil.isBlank(invId))
			{
				ret = expressServer.JoinMerchantById(merc,user ,remark);
			}else
			{
				ret = expressServer.JoinMerchantByCode(merc, user,invId);
			}
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.init("1", "申请失败："+e.getMessage());
		}
		
	}
	
	
	/*
	 * 退出加入企业
	 */
	@RequestMapping(value = { "/companyJoinOut.do" })
	@ResponseBody
	public Result companyJoinOut(HttpServletRequest request, HttpServletResponse response,
			String relaId) 
	{
		try {
			expressServer.JoinOutMerchant(relaId);
			return Result.init("0", "退出成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.init("1", "退出失败：服务器内部错误");
		}
		
	}
	
	/*
	 * 进入邀请码输入页面
	 */
	@RequestMapping(value = { "/companyInvestInput.do" })    
	public ModelAndView companyInvestInput(HttpServletRequest request, HttpServletResponse response,
			String state,String code) 
	{
		request.removeAttribute("merc");
		request.removeAttribute("invId");
		// 通过邀请码去查找
		if(!StringUtil.isEmpty(code))
		{
			Map map = expressServer.getMerchantByInvestCode(WxCooikeManager.getUser(request), code);
			request.setAttribute("merc", map.get("merc"));
			request.setAttribute("invId", map.get("invId"));
		}
		request.setAttribute("code", code);
		return new ModelAndView(PATH+"/company/companyInvestInput");
	}
	
	/*
	 * 进入我的企业列表
	 */
	@RequestMapping(value = { "/companyList.do" })    
	public ModelAndView companyList(HttpServletRequest request, HttpServletResponse response,Query q,Pager page) 
	{
		//找到我加入的企业
		page.setPageSize(5);
		Paginater pageer = null;
		q.setLat(WxCooikeManager.getCookieValue("point.lat", request));
		q.setLng(WxCooikeManager.getCookieValue("point.lng", request));
		
		try {
			q.setUserId(WxCooikeManager.getUser(request).getId());
			pageer = expressServer.getMyJoinedCompany(q, page);
			if(pageer.getMaxRowCount() < 1L)//没有数据
			{
				return new ModelAndView(PATH+"/company/companyListEmpty");
			}
			request.setAttribute("objs", pageer);
			return new ModelAndView(PATH+"/company/companyList");
		} catch (Exception e) {
			return toFail(request, "1", "获取加入的企业列表失败");
		}
	}
	
	
}
